<template>
  <el-dialog :visible.sync="visible" title="审核文件" width="40%" @close="handleClose">
    <el-form :model="form" ref="auditForm" label-width="100px" size="small">
      <el-form-item label="文件名">
        <el-input v-model="form.name" disabled></el-input>
      </el-form-item>
      <el-form-item label="当前状态">
        <el-tag :type="getStatusTagType(form.status)">{{ form.status }}</el-tag>
      </el-form-item>
      <el-form-item label="审核意见">
        <el-select v-model="form.auditStatus" placeholder="请选择审核结果">
          <el-option label="通过" value="已通过"></el-option>
          <el-option label="不通过" value="未通过"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" v-model="form.remark" placeholder="请输入审核备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: "DocumentAudit",
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    file: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      form: {
        id: null,
        name: '',
        status: '',
        auditStatus: '',
        remark: ''
      }
    };
  },
  watch: {
    file: {
      immediate: true,
      handler(newFile) {
        if (newFile) {
          this.form = {
            id: newFile.id,
            name: newFile.name,
            status: newFile.status,
            auditStatus: '',
            remark: ''
          };
        }
      }
    }
  },
  methods: {
    handleClose() {
      this.$emit('update:visible', false);
    },
    getStatusTagType(status) {
      const statusMap = {
        '待审核': 'warning',
        '已通过': 'success',
        '未通过': 'danger'
      };
      return statusMap[status] || '';
    },
    handleSubmit() {
      if (!this.form.auditStatus) {
        this.$message.error('请选择审核结果');
        return;
      }

      // 更新文件状态和备注
      const updatedFile = {
        id: this.form.id,
        status: this.form.auditStatus,
        remark: this.form.remark
      };

      this.$emit('submit', updatedFile);
      this.handleClose();
    }
  }
};
</script>

<style scoped>
.el-dialog__body {
  padding: 20px;
}
</style>
